package com.madrivergroup.mamatodo;

import java.util.ArrayList;
import java.util.List;

import com.madrivergroup.mamatodo.dao.ITaskDao;
import com.madrivergroup.mamatodo.dao.TaskDaoInMemory;
import com.madrivergroup.mamatodo.dao.TaskDaoSqlite;
import com.madrivergroup.mamatodo.dao.TaskDaoSharedPreferences;
import com.madrivergroup.mamatodo.tasks.Task;

import static com.madrivergroup.mamatodo.util.MaMaConstants.*;



import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.util.Log;

/**
 * Acts as the center piece for all Activities within the Application
 * 
 * @author peterdelaney
 *
 */
public class MaMaToDoApplication extends Application {

	public static final String NAME = MaMaToDoApplication.class.getSimpleName();
	
	// Maintains the list of tasks
	//private List<Task> currentTasks = new ArrayList<Task>();
	
	ITaskDao taskDao = null;

	/* (non-Javadoc)
	 * @see android.app.Application#onCreate()
	 */
	@Override
	public void onCreate() {
		Log.d(LOG, NAME + ":onCreate()");
		
		super.onCreate();
		
		taskDao = getDaoImplementation();
	}


	/* (non-Javadoc)
	 * @see android.app.Application#onLowMemory()
	 */
	@Override
	public void onLowMemory() {
		Log.d(LOG, NAME + ":onLowMemory()");		
		super.onLowMemory();
	}


	/* (non-Javadoc)
	 * @see android.app.Application#onTerminate()
	 */
	@Override
	public void onTerminate() {
		Log.d(LOG, NAME + ":onTerminate()");				
		super.onTerminate();
	}
	
	/**
	 * Setter to set the current task list
	 * @param l
	 */
	public void setCurrentTasks( ArrayList<Task> l ) {
		Log.d(LOG, NAME + ":setCurrentTasks()");
		getDaoImplementation().saveTaskList(l);
	}
	
	/**
	 * Return the list of current tasks that are saved
	 * @return Task list
	 */
	public List<Task> getCurrentTasks() {
		Log.d(LOG, NAME + ":getCurrentTasks()");		
		return getDaoImplementation().getTaskList();
	}
	
	/**
	 * Ability to add a new task to the current list of tasks.
	 * @param t new task to add
	 */
	public void addTask( Task t) {
		Log.d(LOG, NAME + ":addTask( " + t + " ) ");
		getDaoImplementation().addTask( t );
	}
	
	/**
	 * Ability to remove a new task to the current list of tasks.
	 * @param t new task to add
	 */
	public void removeTask( Task t) {
		Log.d(LOG, NAME + ":removeTask( " + t + " ) ");
		getDaoImplementation().removeTask( t );
	}
	
	
	/**
	 * Method to create the Proper Implementation
	 * @return
	 */
	protected ITaskDao getDaoImplementation() {
		if( taskDao == null ) {
			
			//taskDao = new TaskDaoSharedPreferences( this );
			//taskDao = new TaskDaoInMemory();
			taskDao = new TaskDaoSqlite( this );
		}
		
		return taskDao;
	}
	
} // end of MaMaToDoApplication class
